package com.zn.listener;

import com.alibaba.fastjson.JSONObject;
import com.zn.util.Constant;
import com.zn.vo.HyDate;
import com.zn.vo.Target;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.List;

/**
 * 订阅redis topic
 */
@Configuration
public class RedisMessageListener {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //初始化监听器
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            @Qualifier("tradeReportNotify")
                                            MessageListenerAdapter tradeReportNotify,
                                            @Qualifier("addReferMdNotify")
                                            MessageListenerAdapter addReferMdNotify,
                                            @Qualifier("replyCustomerMessage")
                                            MessageListenerAdapter replyCustomerMessage,
                                            @Qualifier("updateSocketInfo")
                                            MessageListenerAdapter updateSocketInfo,
                                            @Qualifier("bestsignRegisterStatusNotify")
                                            MessageListenerAdapter bestsignRegisterStatusNotify) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        /**监听交易响应**/
        container.addMessageListener(tradeReportNotify, new PatternTopic(Constant.TRADING_NOTIFY_CHANNEL));
        /**监听新增参考行情**/
        container.addMessageListener(addReferMdNotify, new PatternTopic(Constant.REFER_MD_NOTIFY_CHANNEL));
        /**监听回复消息通道**/
        container.addMessageListener(replyCustomerMessage, new PatternTopic(Constant.CUSTOMER_MESSAGE_CHANNEL));
        /**监听socket信息变更**/
        container.addMessageListener(updateSocketInfo, new PatternTopic(Constant.UPDATE_SOCKET_INFO_CHANNEL));
        /**监听上上签注册状态**/
        container.addMessageListener(bestsignRegisterStatusNotify, new PatternTopic(Constant. BESTSIGN_ASYNCHRONOUS_NOTIFICATION));
        return container;
    }

    /**
     * 交易推送通知
     * @param redisReceiver
     * @return
     */
    @Bean
    @Primary
    MessageListenerAdapter tradeReportNotify(RedisReceiver redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "tradeReportNotify");
    }

    /**
     * 添加参考行情通知
     * @param redisReceiver
     * @return
     */
    @Bean
    MessageListenerAdapter addReferMdNotify(RedisReceiver redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "addReferMdNotify");
    }

    /**
     * 回复消息通知
     * @param redisReceiver
     * @return
     */
    @Bean
    MessageListenerAdapter replyCustomerMessage(RedisReceiver redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "replyCustomerMessage");
    }

    /**
     * 监听变更socketInfo信息
     * @param redisReceiver
     * @return
     */
    @Bean
    MessageListenerAdapter updateSocketInfo(RedisReceiver redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "updateSocketInfo");
    }

    /**
     * 监听变更socketInfo信息
     * @param redisReceiver
     * @return
     */
    @Bean
    MessageListenerAdapter bestsignRegisterStatusNotify(RedisReceiver redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "bestsignRegisterStatusNotify");
    }
}